﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FeedBack {
    public static class Configuration {
        private static string backupFolder;
        private static string inProgressFolder;
        private static int? concurrentDownload;
        private static DateTime? processFileUntil;
        private static string[] skipDomains;
        private static string[] processDomains;
        private static string[] tokens;
        private static int tokenPos;

        public static bool DownloadStatus { get; set; }
        public static bool DownloadLink { get; set; }
        public static bool DownloadVideo { get; set; }
        public static bool DownloadPhoto { get; set; }
        public static bool Overwrite { get; set; }
        public static int Limit { get; set; }
        public static bool SavePagePostOnly { get; set; }

        public static string Token {
            get {
                if (tokens == null) {
                    tokens = ConfigurationManager.AppSettings["Tokens"].Split(',');
                }
                return tokens[tokenPos++ % tokens.Length];
            }
        }

        static Configuration() {
            DownloadPhoto = true;
            DownloadLink = true;
            DownloadVideo = true;
            DownloadStatus = true;
            SavePagePostOnly = false;
            Limit = 25;
        }

        public static string[] ProcessDomains {
            get {
                if (processDomains == null) {
                    var configValue = ConfigurationManager.AppSettings["ProcessDomains"];
                    if (string.IsNullOrWhiteSpace(configValue)) {
                        processDomains = new string[0];
                    }
                    else {
                        processDomains = configValue.Split(';');
                    }
                }
                return processDomains;
            }
        }

        public static string[] SkipDomains {
            get {
                if (skipDomains == null) {
                    var configValue = ConfigurationManager.AppSettings["SkipDomains"];
                    if (string.IsNullOrWhiteSpace(configValue)) {
                        skipDomains = new string[0];
                    }
                    else {
                        skipDomains = configValue.Split(';');
                    }
                }
                return skipDomains;
            }
        }
        
        public static string BackupFolder {
            get {
                if (backupFolder == null) {
                    backupFolder = ConfigurationManager.AppSettings["BackupFolder"];
                    if (string.IsNullOrWhiteSpace(backupFolder)) {
                        backupFolder = Environment.CurrentDirectory;
                    }
                }
                return backupFolder;
            }
        }

        public static string InProgressFolder {
            get {
                if (inProgressFolder == null) {
                    inProgressFolder = ConfigurationManager.AppSettings["InProgressFolder"];
                    if (string.IsNullOrWhiteSpace(inProgressFolder)) {
                        inProgressFolder = Environment.CurrentDirectory;
                    }
                    inProgressFolder = Path.Combine(BackupFolder, inProgressFolder);
                }
                return inProgressFolder;
            }
        }

        public static DateTime ProcessFileUntil {
            get {
                if (!processFileUntil.HasValue) {
                    DateTime result = DateTime.Now.AddHours(1);
                    processFileUntil = result;
                    if (DateTime.TryParse(ConfigurationManager.AppSettings["ProcessFileUntil"], out result)) {
                        processFileUntil = result;
                    }
                }
                return processFileUntil.Value;
            }
        }
        
        public static int ConcurrentDownload {
            get {
                if (!concurrentDownload.HasValue) {
                    int result = 4;
                    concurrentDownload = result;
                    if (int.TryParse(ConfigurationManager.AppSettings["ConcurrentDownload"], out result)) {
                        concurrentDownload = result;
                    }
                }
                return concurrentDownload.Value;
            }
        }

    }
}
